<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Models\StaffOrder;
use App\Models\CarAuth;
use App\Services\CarAuthServices;
use Excel;


class StaffCarAuthFix extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'staff:car-auth-fix';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '员工车批量授权-fix';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // $orders = StaffOrder::where('status', 1)
        //                     ->orderBy('id', 'desc')
        //                     ->get();


        // $this->removeOld(9);
        // $this->issueAuthAll();


        $this->issueAuthStaff();



        // // send sms
        // $carAuth = new CarAuthServices;

        // // orders
        // $n = 0;
        // foreach ($orders as $order) {
        //     if (CarAuth::where('vpl_number', $order->vpl_number)->count() > 0) {
        //         $this->info('EXIST: ' . $order->vpl_number);
        //         continue;
        //     }

        //     $this->info(sprintf('NEW: %04d. %s', ++$n, $order->vpl_number));

        //     // delete old auths
        //     // $carAuth->deleteByVpl( $order->vpl_number );
        //     // sleep(2);
        //     $carAuth->issueAuth( $order );
        //     // return ;
        //     sleep(5);
        // }

    }


    private function removeOld($type = 9)
    {
        $n = 0;
        $park_code = [
            '7100000285',
            '7100000256',
        ];
        foreach ($this->vpl_numbers as $vpl) {

            if ($item = CarAuth::where('vpl_number', $vpl)
                        ->whereIn('park_code', $park_code)
                        ->first() ) {
                $this->info(
                    sprintf("%04d: %s - %s - %s - %s",
                        ++$n,
                        $item->id,
                        $item->vpl_number,
                        $item->validbtime,
                        $item->validetime
                    )
                );

                CarAuth::where('id', $item->id)->delete();
            }
        }
    }



    private function issueAuthAll()
    {

        $carAuth = new CarAuthServices;

        Excel::load('cars.xlsx', function($reader) use ($carAuth) {
            $reader = $reader->getSheet(0);
            $rs = $reader->toArray();
            // 0 车牌号  1共享车位 2 授权类型  3 起始时间 4 结束日期 5 电话 6 所属机构
            unset($rs[0]); // 注销文件首行

            $carAuths = [];
            $auth_num = 0;
            foreach ($rs as $re) {
                $name = $re[0];
                $vpl_number  = $re[1];
                $mobile  = $re[6];

                $carAuth->issueAuthAll($vpl_number, $mobile, $name);
                $this->info($vpl_number);

                sleep(1);
                // return;
            }
        });
    }


    private function issueAuthStaff()
    {

        $carAuth = new CarAuthServices;
        $parks = [
            // "P3银联-7100000013" => "7100000013", 
            // "P1职工-7100000285" => "7100000285", 
            "P3-7100000225" => "7100000225", 
            "P2-7100000215" => "7100000215", 
            // "P6-7100000183" => "7100000183", 
            "P1-7100000200" => "7100000200", 
            "PBUS-7100000226" => "7100000226", 
            "P20-7100000228" => "7100000228", 
            // "P5-7100000254" => "7100000254", 
            // "P4-7100000252" => "7100000252", 
            // "P8-7100000256" => "7100000256",
            // "PVVIP2-7100000258" => "7100000258",
            // "PVIP3-7100000259" => "7100000259",
            "P12-7100000201" => "7100000201",
            // "PVVIP3-7100000260" => "7100000260",
            // "P8-7100000256" => "7100000256",
            // "all" => "all",
        ];

        foreach ($parks as $filename => $park) {
            $park_code = ($park == 'all') 
                            ? config('custom.staff_auth_parks')
                            : [$park];

            $filename = 'delete';
            Excel::load(sprintf("parks/%s.xlsx", $filename), 
                            function($reader) use ($carAuth, $filename, $park, $park_code) {
                $reader = $reader->getSheet(0);
                $rs = $reader->toArray();
                // 0 车牌号  1共享车位 2 授权类型  3 起始时间 4 结束日期 5 电话 6 所属机构
                unset($rs[0]); // 注销文件首行

                $n = 0;
                foreach ($rs as $re) {
                    // $n++;
                    $name = trim($re[0]);
                    $vpl_number  = str_replace(' ', '', trim($re[1]));
                    $mobile  = trim($re[6]);
                    $type = trim($re[3]);
                    // $type = 9;
                    $mobile = $mobile ?: ''; 

                    if (empty($vpl_number)) {
                        continue;
                    }

                    // $this->info(
                    //     sprintf("%04d: %s - %s - %s - %11s - %s",
                    //         $n, $filename, $type, $vpl_number, $mobile, $name
                    //     )
                    // );


                    $carAuth = CarAuth::where('park_code', $park)
                                    ->select('id', 'type')
                                    ->where('vpl_number', $vpl_number)
                                    ->where('type', 9)
                                    ->first();
                    if ($carAuth) {
                        CarAuth::where('id', $carAuth->id)->delete();
                        $type = $carAuth->type;
                        $n++;
                        $this->info(
                            sprintf("%04d: %s - %s - %s - %11s - %s",
                                $n, $carAuth->id, $type, $vpl_number, $mobile, $name
                            )
                        );
                    }
                    // $carAuth->issueAuthFix($park_code, $vpl_number, $mobile, $name, $type);

                    // sleep(1);
                }
            });

            // return;
        }
    }

}
